package spring.cloud.eurekaclientuser.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import spring.cloud.eurekaclientuser.genderid.CustomWorker;
import spring.cloud.eurekaclientuser.genderid.RedisGenerateId;
import spring.cloud.vo.ResultMessage;
import spring.cloud.vo.UserInfo;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Controller
@RequestMapping("/user")
public class UserInfoContrller {


    @RequestMapping("/info/{id}")
    @ResponseBody
    public UserInfo getUserInfo(@PathVariable("id") Long id) throws InterruptedException {

        if(id == 0){
//            throw new RuntimeException("异常");
            Thread.sleep(20000);
        }


        return new UserInfo(1L,"user_name_"+id,"note_"+id);
    }

    @PutMapping("/info")
    @ResponseBody
    public UserInfo putUserInfo(@RequestBody UserInfo userInfo){
        return userInfo;
    }

    @GetMapping("/infoes/{ids}")
    public ResponseEntity<List<UserInfo>>findUsers(@PathVariable("ids") Long[] ids){
        List<UserInfo> userList = new ArrayList<>();
        for(Long id:ids){
            UserInfo userInfo = new UserInfo(id,"user_name_"+id,"note_"+id);
            userList.add(userInfo);
        }
        ResponseEntity<List<UserInfo>> response = new ResponseEntity<>(userList, HttpStatus.OK);
        return response;
    }


    @GetMapping("/infoes2")
    @ResponseBody
    public ResponseEntity<List<UserInfo>> findUser2(@RequestParam("ids") Long []ids){
        List<UserInfo> userInfoList =  new ArrayList<>();
        for(Long id:ids){
            UserInfo userInfo = new UserInfo(id,"user_name_"+id,"note+"+id);
            userInfoList.add(userInfo);
        }
        ResponseEntity<List<UserInfo>> response = new ResponseEntity<>(userInfoList,HttpStatus.OK);
        return response;
    }

    @DeleteMapping("/info")
    @ResponseBody
    public ResultMessage deleteUser(@RequestHeader("id") Long id){

        boolean success = id != null;
        String msg = success?"传递成功":"传递失败";
        return new ResultMessage(success,msg);
    }

    @PostMapping("/upload")
    @ResponseBody
    public ResultMessage uploadFile(@RequestPart("file") MultipartFile file){

        boolean success = file != null && file.getSize()>0;
        String message =  success?"文件传递成功":"文件传递失败";
        return new ResultMessage(success,message);
    }

    @Autowired
    private StringRedisTemplate stringRedisTemplate = null;

    @GetMapping("/id/{keyType}")
    public String redisHashId(@PathVariable("keyType") String keyType){

        RedisGenerateId redisGenerateId = new RedisGenerateId(stringRedisTemplate);
        System.out.println(redisGenerateId.getRedisHashId(keyType));

        return "";
    }

    //雪花发好机制
    @GetMapping("/snowflake")
    @ResponseBody
    public Map<String,String> snowflakework(){

//        SnowFlakeWorker worker = new SnowFlakeWorker(100L);

        CustomWorker worker = new CustomWorker(100L);
        Long id = worker.nextId();
        System.out.println(id);

        Map<String,String> map = new HashMap<>();
        map.put("result",id+"");
        return map;
    }
}
